package com.qfedu.yanglao20admin.equipment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.equipment.FacilityManage;
import com.qfedu.yanglao20admin.equipment.common.RunEnum;
import com.qfedu.yanglao20admin.equipment.common.StateEnum;
import com.qfedu.yanglao20admin.equipment.dao.FacilityManageDao;
import com.qfedu.yanglao20admin.equipment.param.FacilityManageParam;
import com.qfedu.yanglao20admin.equipment.service.FacilityManageService;
import com.qfedu.yanglao20admin.equipment.vo.FacilityManageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/26 17:03
 */
@Service
public class FacilityManageServiceImpl implements FacilityManageService {

    @Resource
    private FacilityManageDao facilityManageDao;

    @Override
    public Page<FacilityManageVO> pageFacilityManage(FacilityManageParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<FacilityManageVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<FacilityManageVO> facilityManageVOS = facilityManageDao.selectList(param);

        //  循环遍历
        for (FacilityManageVO vo :
                facilityManageVOS) {
            StateEnum stateById = StateEnum.getStateById(vo.getStateFlag());
            RunEnum runById = RunEnum.getRunById(vo.getRunFlag());
            vo.setStateDesc(stateById.getDesc());
            vo.setRunDesc(runById.getDesc());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = facilityManageDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(facilityManageVOS);

        return page;
    }

    @Override
    public void addFacilityManage(FacilityManage facilityManage) {

        if (facilityManage.getFacilityNum() == null || facilityManage.getFacilityNum().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写设备编号");
        }

        FacilityManage byfacilityNum =
                facilityManageDao.selectByfacilityNum(facilityManage.getFacilityNum());
        if (byfacilityNum != null) {
            throw new CommonException(CommonConstants.ERROR, "该设备编号已存在");
        }

        FacilityManage byName =
                facilityManageDao.selectByName(facilityManage.getFacilityName());

        if (byName != null) {
            throw new CommonException(CommonConstants.ERROR, "该设备名称已存在");
        }

        facilityManageDao.insert(facilityManage);
    }

    @Override
    public void updateFacilityManage(FacilityManage facilityManage) {

        if (facilityManage.getFacilityNum() == null || facilityManage.getFacilityNum().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写设备编号");
        }

        FacilityManage byfacilityNum =
                facilityManageDao.selectByfacilityNum(facilityManage.getFacilityNum());
        if (byfacilityNum != null) {
            if (!facilityManage.getId().equals(byfacilityNum.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该设备编号已存在");
            }
        }

        FacilityManage byName =
                facilityManageDao.selectByName(facilityManage.getFacilityName());
        if (byName != null) {
            if (!byName.getId().equals(facilityManage.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该设备名称已存在");
            }
        }

        facilityManageDao.updateById(facilityManage);
    }

    @Override
    public FacilityManage queryFacilityManage(Integer id) {
        return facilityManageDao.selectById(id);
    }

    @Override
    public void deleteById(Integer id) {
        facilityManageDao.deleteById(id);
    }

    @Override
    public void updatestateFlag(Integer id) {
        facilityManageDao.updatestateFlag(id);
    }
}
